package com.lzq.mq.common.remote;

import com.lzq.mq.common.cache.NameServerSyncFutureManager;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class SyncFuture implements Future {

    /**
     * 远程rpc返回的数据内容
     */
    @Getter
    private Object response;
    @Setter
    @Getter
    private String msgId;

    private CountDownLatch countDownLatch = new CountDownLatch(1);

    public void setResponse(Object response) {
        this.response = response;
        countDownLatch.countDown();
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return false;
    }

    @Override
    public boolean isCancelled() {
        return false;
    }

    @Override
    public boolean isDone() {
        return response != null;
    }

    @Override
    public Object get() throws InterruptedException, ExecutionException {
        countDownLatch.await();
        NameServerSyncFutureManager.remove(msgId);
        return response;
    }

    @Override
    public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException {
        boolean isDone = countDownLatch.await(timeout, unit);
        NameServerSyncFutureManager.remove(msgId);
        return isDone ? response : null;
    }
}
